package com.remoter.monitor.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.remoter.basic.bean.support.ConsumerModule;
import com.remoter.basic.bean.support.ProviderModule;
import com.remoter.basic.configure.IConfiguration;
import com.remoter.basic.configure.support.AbstractConfiguration;
import com.remoter.basic.registry.IRegistry;
import com.remoter.basic.util.Final;
import com.remoter.basic.util.ServiceLoader;
import com.remoter.basic.util.StringUtil;
import com.remoter.monitor.service.IMonitorService;

/**
 * @author			koko
 * @date 			2017-08-11 09:50:13
 * @description 	类功能说明
 */
@Service
public class MonitorServiceImpl implements IMonitorService{
	
	private IConfiguration configuration;
	private IRegistry registry;
	
	@PostConstruct
	public void init(){
		this.configuration = AbstractConfiguration.getConfiguration();
		this.registry = ServiceLoader.getService(IRegistry.class,this.configuration.getOption(Final.O_VAL_REGISTRY));
	}

	@Override
	public JSONObject servers(){
		JSONObject result = new JSONObject();
		List<ProviderModule> providers = this.registry.listProviders();
		List<ConsumerModule> consumers = this.registry.listConsumers();
		if(null != providers && providers.size() > 0){
			for(ProviderModule module : providers){
				JSONObject object = result.getJSONObject(module.getServer());
				if(null == object){
					result.put(module.getServer(),new JSONObject());
					object = result.getJSONObject(module.getServer());
				}
				String key = String.format("%s-%s-%s",module.getProtocol(),module.getHost(),module.getPort());
				if(object.containsKey(key)){
					continue;
				}
				JSONObject item = new JSONObject();
				item.put("protocol",module.getProtocol());
				item.put("host",module.getHost());
				item.put("port",module.getPort());
				item.put("provider",true);
				item.put("online",true);
				object.put(key,item);
			}
		}
		if(null != consumers && consumers.size() > 0){
			for(ConsumerModule module : consumers){
				JSONObject object = result.getJSONObject(module.getServer());
				if(null == object){
					result.put(module.getServer(),new JSONObject());
					object = result.getJSONObject(module.getServer());
				}
				String key = String.format("%s-%s-%s",module.getProtocol(),module.getHost(),module.getPort());
				if(object.containsKey(key)){
					continue;
				}
				JSONObject item = new JSONObject();
				item.put("protocol",module.getProtocol());
				item.put("host",module.getHost());
				item.put("port",module.getPort());
				item.put("consumer",true);
				if(module.getPort() == 0){
					item.put("online",false);
				}else{
					item.put("online",true);
				}
				object.put(key,item);
			}
		}
		return result;
	}

	@Override
	public JSONObject services() {
		JSONObject result = new JSONObject();
		List<ProviderModule> providers = this.registry.listProviders();
		List<ConsumerModule> consumers = this.registry.listConsumers();
		if(null != providers && providers.size() > 0){
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			for(ProviderModule module : providers){
				String key = String.format("%s://%s(%s)",module.getProtocol(),module.getType(),module.getBean());
				JSONObject object = result.getJSONObject(key);
				if(null == object){
					result.put(key,new JSONObject());
					object = result.getJSONObject(key);
				}
				String childKey = String.format("%s:%s",module.getHost(),module.getPort());
				if(object.containsKey(childKey)){
					continue;
				}
				JSONObject item = new JSONObject();
				item.put("protocol",module.getProtocol());
				item.put("host",module.getHost());
				item.put("port",module.getPort());
				item.put("provider",true);
				item.put("online",true);
				item.put("time",sdf.format(new Date(module.getStartTime())));
				object.put(childKey,item);
			}
		}
		if(null != consumers && consumers.size() > 0){
			for(ConsumerModule module : consumers){
				String key = String.format("%s://%s(%s)",module.getProtocol(),module.getType(),module.getBean());
				JSONObject object = result.getJSONObject(key);
				if(null == object){
					result.put(key,new JSONObject());
					object = result.getJSONObject(key);
				}
				String childKey = String.format("%s:%s",module.getHost(),module.getPort());
				if(object.containsKey(childKey)){
					continue;
				}
				JSONObject item = new JSONObject();
				item.put("protocol",module.getProtocol());
				item.put("host",module.getHost());
				item.put("port",module.getPort());
				item.put("consumer",true);
				if(module.getPort() == 0){
					item.put("online",false);
				}else{
					item.put("online",true);
				}
				object.put(childKey,item);
			}
		}
		return result;
	}

	@Override
	public JSONArray providerDetail(String protocol, String type, String bean){
		JSONArray result = new JSONArray();
		List<ProviderModule> providers = this.registry.listProviders();
		for(ProviderModule module : providers){
			if(
				StringUtil.isEquals(protocol,module.getProtocol()) && 
				StringUtil.isEquals(type,module.getType()) && 
				StringUtil.isEquals(bean,module.getBean())
			){
				result.add(module);
			}
		}
		return result;
	}
	
	@Override
	public JSONArray providerDetail(String protocol) {
		JSONArray result = new JSONArray();
		List<ProviderModule> providers = this.registry.listProviders();
		for(ProviderModule module : providers){
			if(StringUtil.isEquals(protocol,module.getProtocol())){
				result.add(module);
			}
		}
		return result;
	}

	@Override
	public JSONArray consumerDetail(String protocol, String type, String bean) {
		JSONArray result = new JSONArray();
		List<ConsumerModule> consumers = this.registry.listConsumers();
		for(ConsumerModule module : consumers){
			if(
				StringUtil.isEquals(protocol,module.getProtocol()) && 
				StringUtil.isEquals(type,module.getType()) && 
				StringUtil.isEquals(bean,module.getBean())
			){
				result.add(module);
			}
		}
		return result;
	}

	@Override
	public JSONArray consumerDetail(String protocol) {
		JSONArray result = new JSONArray();
		List<ConsumerModule> consumers = this.registry.listConsumers();
		for(ConsumerModule module : consumers){
			if(StringUtil.isEquals(protocol,module.getProtocol())){
				result.add(module);
			}
		}
		return result;
	}
	
}